{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.7.3-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python37364bitbaseconda6fac67c13ee249de96fe454f62091838",
   "display_name": "Python 3.7.3 64-bit ('base': conda)"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>首次投资时间</th>\n      <th>最近一次投资时间</th>\n      <th>总计投资总金额</th>\n      <th>总计投标总次数</th>\n    </tr>\n    <tr>\n      <th>用户编码</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>45B3CCE7-957B-4D54-9626-6D62731D119B</th>\n      <td>2016-06-25</td>\n      <td>2016-06-26</td>\n      <td>20000.0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>9921C535-75FC-4695-8551-32B8D717B4A5</th>\n      <td>2016-06-26</td>\n      <td>2016-06-26</td>\n      <td>50000.0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2F00D4E3-CC7D-4DB1-B210-6F095156C5B8</th>\n      <td>2016-06-13</td>\n      <td>2016-06-19</td>\n      <td>108000.0</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>150218D8-BF32-406D-A3A8-8A19CB1E85AA</th>\n      <td>2016-06-16</td>\n      <td>2016-06-16</td>\n      <td>30000.0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>98C26D4A-708F-4334-B3D7-EDAE9FD240C8</th>\n      <td>2016-06-27</td>\n      <td>2016-06-27</td>\n      <td>100000.0</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "                                         首次投资时间   最近一次投资时间   总计投资总金额  总计投标总次数\n用户编码                                                                         \n45B3CCE7-957B-4D54-9626-6D62731D119B 2016-06-25 2016-06-26   20000.0        3\n9921C535-75FC-4695-8551-32B8D717B4A5 2016-06-26 2016-06-26   50000.0        3\n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8 2016-06-13 2016-06-19  108000.0        4\n150218D8-BF32-406D-A3A8-8A19CB1E85AA 2016-06-16 2016-06-16   30000.0        3\n98C26D4A-708F-4334-B3D7-EDAE9FD240C8 2016-06-27 2016-06-27  100000.0        3"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "data = pd.read_excel('RFM聚类分析【样本数据】.xlsx', index_col='用户编码')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "200"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据探索及预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>首次投资时间</th>\n      <th>最近一次投资时间</th>\n      <th>总计投资总金额</th>\n      <th>总计投标总次数</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>200</td>\n      <td>200</td>\n      <td>200.000000</td>\n      <td>200.00000</td>\n    </tr>\n    <tr>\n      <th>unique</th>\n      <td>24</td>\n      <td>38</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>top</th>\n      <td>2016-06-22 00:00:00</td>\n      <td>2016-06-25 00:00:00</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>freq</th>\n      <td>20</td>\n      <td>25</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>first</th>\n      <td>2016-06-06 00:00:00</td>\n      <td>2016-06-06 00:00:00</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>last</th>\n      <td>2016-06-29 00:00:00</td>\n      <td>2016-07-18 00:00:00</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>47223.409500</td>\n      <td>5.92000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>43381.496372</td>\n      <td>4.84883</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>10100.000000</td>\n      <td>2.00000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>20000.000000</td>\n      <td>3.00000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>30000.000000</td>\n      <td>4.00000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>58000.000000</td>\n      <td>6.00000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>250000.000000</td>\n      <td>34.00000</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "                     首次投资时间             最近一次投资时间        总计投资总金额    总计投标总次数\ncount                   200                  200     200.000000  200.00000\nunique                   24                   38            NaN        NaN\ntop     2016-06-22 00:00:00  2016-06-25 00:00:00            NaN        NaN\nfreq                     20                   25            NaN        NaN\nfirst   2016-06-06 00:00:00  2016-06-06 00:00:00            NaN        NaN\nlast    2016-06-29 00:00:00  2016-07-18 00:00:00            NaN        NaN\nmean                    NaN                  NaN   47223.409500    5.92000\nstd                     NaN                  NaN   43381.496372    4.84883\nmin                     NaN                  NaN   10100.000000    2.00000\n25%                     NaN                  NaN   20000.000000    3.00000\n50%                     NaN                  NaN   30000.000000    4.00000\n75%                     NaN                  NaN   58000.000000    6.00000\nmax                     NaN                  NaN  250000.000000   34.00000"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe(include='all')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "2016-07-20 00:00:00\n"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "exdata_date = datetime(2016,7,20)\n",
    "print(exdata_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "用户编码\n45B3CCE7-957B-4D54-9626-6D62731D119B   24 days\n9921C535-75FC-4695-8551-32B8D717B4A5   24 days\n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8   31 days\n150218D8-BF32-406D-A3A8-8A19CB1E85AA   34 days\n98C26D4A-708F-4334-B3D7-EDAE9FD240C8   23 days\nName: 最近一次投资时间, dtype: timedelta64[ns]"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# R最近一次投资时间据提数日天数\n",
    "diff_R = exdata_date-data['最近一次投资时间']\n",
    "diff_R.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "24"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff_R[0].days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "int"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(diff_R[0].days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "[24, 24, 31, 34, 23, 28, 39, 22, 38, 25]"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R = []\n",
    "for i in diff_R:\n",
    "    R.append(i.days)\n",
    "\n",
    "R[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "用户编码\n45B3CCE7-957B-4D54-9626-6D62731D119B   25 days\n9921C535-75FC-4695-8551-32B8D717B4A5   24 days\n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8   37 days\n150218D8-BF32-406D-A3A8-8A19CB1E85AA   34 days\n98C26D4A-708F-4334-B3D7-EDAE9FD240C8   23 days\nName: 首次投资时间, dtype: timedelta64[ns]"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "用户在投时长（月）\n",
    "1、用户在投时长（天）\n",
    "2、月=天/30,向上取整\n",
    "'''\n",
    "diff = exdata_date-data['首次投资时间']\n",
    "diff.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0.8333333333333334"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff[0].days/30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ceil ,向上取整\n",
    "from math import ceil\n",
    "\n",
    "ceil(diff[0].days/30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "[25, 24, 37, 34, 23, 28, 41, 22, 38, 25]"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取相差天数\n",
    "diff_days = []\n",
    "for i in diff:\n",
    "    diff_days.append(i.days)\n",
    "\n",
    "diff_days[:10]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "[1, 1, 2, 2, 1, 1, 2, 1, 2, 1]"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过循环向上取整，得到用户在投时长（月）\n",
    "diff_months = []\n",
    "\n",
    "for i in diff_days:\n",
    "    diff_months.append(ceil(i/30))\n",
    "\n",
    "diff_months[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "用户编码\n45B3CCE7-957B-4D54-9626-6D62731D119B    3.0\n9921C535-75FC-4695-8551-32B8D717B4A5    3.0\n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8    2.0\n150218D8-BF32-406D-A3A8-8A19CB1E85AA    1.5\n98C26D4A-708F-4334-B3D7-EDAE9FD240C8    3.0\nName: 总计投标总次数, dtype: float64"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#F, 月均投资次数\n",
    "F=data['总计投标总次数']/diff_months\n",
    "F.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([3. , 3. , 2. , 1.5, 3. , 3. , 1.5, 3. , 1. , 3. ])"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F.values[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 20000.,  50000.,  54000.,  15000., 100000.,  15000.,  10000.,\n        16000.,   7500.,  20000.])"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# M, 月均投资金额\n",
    "M = (data['总计投资总金额']/diff_months).values\n",
    "M[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>24.0</td>\n      <td>3.0</td>\n      <td>20000.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>24.0</td>\n      <td>3.0</td>\n      <td>50000.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>31.0</td>\n      <td>2.0</td>\n      <td>54000.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34.0</td>\n      <td>1.5</td>\n      <td>15000.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>23.0</td>\n      <td>3.0</td>\n      <td>100000.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "      0    1         2\n0  24.0  3.0   20000.0\n1  24.0  3.0   50000.0\n2  31.0  2.0   54000.0\n3  34.0  1.5   15000.0\n4  23.0  3.0  100000.0"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取 R、F、M 三个指标作为聚类分析指标\n",
    "from pandas import DataFrame\n",
    "\n",
    "cdata=DataFrame([R,list(F),list(M)]).T\n",
    "cdata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>R-最近一次投资时间据提数日的天数</th>\n      <th>F-月均投资次数</th>\n      <th>M-月均投资金额</th>\n    </tr>\n    <tr>\n      <th>用户编码</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>45B3CCE7-957B-4D54-9626-6D62731D119B</th>\n      <td>24.0</td>\n      <td>3.0</td>\n      <td>20000.0</td>\n    </tr>\n    <tr>\n      <th>9921C535-75FC-4695-8551-32B8D717B4A5</th>\n      <td>24.0</td>\n      <td>3.0</td>\n      <td>50000.0</td>\n    </tr>\n    <tr>\n      <th>2F00D4E3-CC7D-4DB1-B210-6F095156C5B8</th>\n      <td>31.0</td>\n      <td>2.0</td>\n      <td>54000.0</td>\n    </tr>\n    <tr>\n      <th>150218D8-BF32-406D-A3A8-8A19CB1E85AA</th>\n      <td>34.0</td>\n      <td>1.5</td>\n      <td>15000.0</td>\n    </tr>\n    <tr>\n      <th>98C26D4A-708F-4334-B3D7-EDAE9FD240C8</th>\n      <td>23.0</td>\n      <td>3.0</td>\n      <td>100000.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "                                      R-最近一次投资时间据提数日的天数  F-月均投资次数  M-月均投资金额\n用户编码                                                                       \n45B3CCE7-957B-4D54-9626-6D62731D119B               24.0       3.0   20000.0\n9921C535-75FC-4695-8551-32B8D717B4A5               24.0       3.0   50000.0\n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8               31.0       2.0   54000.0\n150218D8-BF32-406D-A3A8-8A19CB1E85AA               34.0       1.5   15000.0\n98C26D4A-708F-4334-B3D7-EDAE9FD240C8               23.0       3.0  100000.0"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#指定cdata的index,columns\n",
    "cdata.index = data.index\n",
    "cdata.columns = ['R-最近一次投资时间据提数日的天数', 'F-月均投资次数', 'M-月均投资金额']\n",
    "cdata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "R-最近一次投资时间据提数日的天数       22.66500\nF-月均投资次数                 4.24250\nM-月均投资金额             35821.24175\ndtype: float64"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cdata.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>R-最近一次投资时间据提数日的天数</th>\n      <th>F-月均投资次数</th>\n      <th>M-月均投资金额</th>\n    </tr>\n    <tr>\n      <th>用户编码</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>45B3CCE7-957B-4D54-9626-6D62731D119B</th>\n      <td>0.140422</td>\n      <td>-0.353751</td>\n      <td>-0.392133</td>\n    </tr>\n    <tr>\n      <th>9921C535-75FC-4695-8551-32B8D717B4A5</th>\n      <td>0.140422</td>\n      <td>-0.353751</td>\n      <td>0.351423</td>\n    </tr>\n    <tr>\n      <th>2F00D4E3-CC7D-4DB1-B210-6F095156C5B8</th>\n      <td>0.876714</td>\n      <td>-0.638461</td>\n      <td>0.450564</td>\n    </tr>\n    <tr>\n      <th>150218D8-BF32-406D-A3A8-8A19CB1E85AA</th>\n      <td>1.192268</td>\n      <td>-0.780815</td>\n      <td>-0.516059</td>\n    </tr>\n    <tr>\n      <th>98C26D4A-708F-4334-B3D7-EDAE9FD240C8</th>\n      <td>0.035237</td>\n      <td>-0.353751</td>\n      <td>1.590683</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "                                      R-最近一次投资时间据提数日的天数  F-月均投资次数  M-月均投资金额\n用户编码                                                                       \n45B3CCE7-957B-4D54-9626-6D62731D119B           0.140422 -0.353751 -0.392133\n9921C535-75FC-4695-8551-32B8D717B4A5           0.140422 -0.353751  0.351423\n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8           0.876714 -0.638461  0.450564\n150218D8-BF32-406D-A3A8-8A19CB1E85AA           1.192268 -0.780815 -0.516059\n98C26D4A-708F-4334-B3D7-EDAE9FD240C8           0.035237 -0.353751  1.590683"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zcdata=(cdata-cdata.mean())/cdata.std()\n",
    "zcdata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=100,\n       n_clusters=4, n_init=10, n_jobs=4, precompute_distances='auto',\n       random_state=0, tol=0.0001, verbose=0)"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#sklearn\n",
    "from sklearn.cluster import KMeans\n",
    "\n",
    "kmodel=KMeans(n_clusters=4,n_jobs=4,max_iter=100,random_state=0)\n",
    "kmodel.fit(zcdata)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,\n       0, 0, 2, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0,\n       0, 0, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 3,\n       0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 2, 0, 1, 0, 2, 0, 0, 2, 3, 2,\n       2, 2, 2, 0, 1, 0, 2, 0, 2, 2, 2, 0, 1, 0, 3, 1, 0, 2, 2, 0, 0, 0,\n       2, 0, 0, 0, 2, 2, 3, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,\n       3, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 1, 0, 2, 0,\n       3, 2, 2, 0, 0, 0, 2, 1, 0, 0, 0, 1, 0, 0, 2, 2, 0, 0, 0, 1, 2, 1,\n       2, 0, 2, 0, 0, 1, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 2, 2,\n       0, 0], dtype=int32)"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmodel.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0    128\n2     44\n1     19\n3      9\ndtype: int64"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#统计每个类别的频率\n",
    "\n",
    "from pandas import Series\n",
    "\n",
    "Series(kmodel.labels_).value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>R-最近一次投资时间据提数日的天数</th>\n      <th>F-月均投资次数</th>\n      <th>M-月均投资金额</th>\n      <th>0</th>\n    </tr>\n    <tr>\n      <th>用户编码</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>45B3CCE7-957B-4D54-9626-6D62731D119B</th>\n      <td>24.0</td>\n      <td>3.0</td>\n      <td>20000.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9921C535-75FC-4695-8551-32B8D717B4A5</th>\n      <td>24.0</td>\n      <td>3.0</td>\n      <td>50000.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2F00D4E3-CC7D-4DB1-B210-6F095156C5B8</th>\n      <td>31.0</td>\n      <td>2.0</td>\n      <td>54000.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>150218D8-BF32-406D-A3A8-8A19CB1E85AA</th>\n      <td>34.0</td>\n      <td>1.5</td>\n      <td>15000.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>98C26D4A-708F-4334-B3D7-EDAE9FD240C8</th>\n      <td>23.0</td>\n      <td>3.0</td>\n      <td>100000.0</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "                                      R-最近一次投资时间据提数日的天数  F-月均投资次数  M-月均投资金额  0\n用户编码                                                                          \n45B3CCE7-957B-4D54-9626-6D62731D119B               24.0       3.0   20000.0  0\n9921C535-75FC-4695-8551-32B8D717B4A5               24.0       3.0   50000.0  0\n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8               31.0       2.0   54000.0  0\n150218D8-BF32-406D-A3A8-8A19CB1E85AA               34.0       1.5   15000.0  0\n98C26D4A-708F-4334-B3D7-EDAE9FD240C8               23.0       3.0  100000.0  1"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将类别标签赋回原数据\n",
    "cdata_rst = pd.concat([cdata, Series(kmodel.labels_,index=cdata.index)],axis=1)\n",
    "cdata_rst.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>R-最近一次投资时间据提数日的天数</th>\n      <th>F-月均投资次数</th>\n      <th>M-月均投资金额</th>\n      <th>类别</th>\n    </tr>\n    <tr>\n      <th>用户编码</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>45B3CCE7-957B-4D54-9626-6D62731D119B</th>\n      <td>24.0</td>\n      <td>3.0</td>\n      <td>20000.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9921C535-75FC-4695-8551-32B8D717B4A5</th>\n      <td>24.0</td>\n      <td>3.0</td>\n      <td>50000.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2F00D4E3-CC7D-4DB1-B210-6F095156C5B8</th>\n      <td>31.0</td>\n      <td>2.0</td>\n      <td>54000.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>150218D8-BF32-406D-A3A8-8A19CB1E85AA</th>\n      <td>34.0</td>\n      <td>1.5</td>\n      <td>15000.0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>98C26D4A-708F-4334-B3D7-EDAE9FD240C8</th>\n      <td>23.0</td>\n      <td>3.0</td>\n      <td>100000.0</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "                                      R-最近一次投资时间据提数日的天数  F-月均投资次数  M-月均投资金额  \\\n用户编码                                                                          \n45B3CCE7-957B-4D54-9626-6D62731D119B               24.0       3.0   20000.0   \n9921C535-75FC-4695-8551-32B8D717B4A5               24.0       3.0   50000.0   \n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8               31.0       2.0   54000.0   \n150218D8-BF32-406D-A3A8-8A19CB1E85AA               34.0       1.5   15000.0   \n98C26D4A-708F-4334-B3D7-EDAE9FD240C8               23.0       3.0  100000.0   \n\n                                      类别  \n用户编码                                      \n45B3CCE7-957B-4D54-9626-6D62731D119B   0  \n9921C535-75FC-4695-8551-32B8D717B4A5   0  \n2F00D4E3-CC7D-4DB1-B210-6F095156C5B8   0  \n150218D8-BF32-406D-A3A8-8A19CB1E85AA   0  \n98C26D4A-708F-4334-B3D7-EDAE9FD240C8   1  "
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#命名最后一列为类别\n",
    "cdata_rst.columns=list(cdata.columns)+['类别']\n",
    "cdata_rst.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>R-最近一次投资时间据提数日的天数</th>\n      <th>F-月均投资次数</th>\n      <th>M-月均投资金额</th>\n    </tr>\n    <tr>\n      <th>类别</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>27.859375</td>\n      <td>2.820312</td>\n      <td>21906.754297</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>20.684211</td>\n      <td>4.552632</td>\n      <td>115842.105263</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>10.568182</td>\n      <td>5.579545</td>\n      <td>26984.313636</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>12.111111</td>\n      <td>17.277778</td>\n      <td>107986.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "    R-最近一次投资时间据提数日的天数   F-月均投资次数       M-月均投资金额\n类别                                             \n0           27.859375   2.820312   21906.754297\n1           20.684211   4.552632  115842.105263\n2           10.568182   5.579545   26984.313636\n3           12.111111  17.277778  107986.000000"
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#按照类别分组统计R、F、M指标的均值\n",
    "#groupby\n",
    "cdata_rst.groupby(cdata_rst['类别']).mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**结论：**\n",
    "\n",
    "- 类别3： R、F、M都比较高，数据重要价值客户，或者超级用户\n",
    "- 类别0： R、F、M都比较低，属于低价值客户\n",
    "- 类别1： M很高，F还行, R不高，保持客户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}